package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AirCraftServiceImpl implements IAirCraftService {
    @Autowired
    private AirCraftMapper airCraftMapper;
    List<AirCraft> list = new ArrayList<>();

    @Override
    public AirCraft add(AirCraft airCraft) {
        ValidAirPort(airCraft);
        String AirIcao = airCraft.getIcaoCode();
        if (StrUtil.isBlank(AirIcao)) {
            throw new RuntimeException("飞机地址编码不能为空");
        }
        String register = airCraft.getRegistration();
        if (StrUtil.isBlank(register) || register.length() > 7) {
            throw new RuntimeException("飞机注册号不能超过7位且不能为空");
        }
        existingRegister(airCraft.getRegistration());
        airCraft.setCreateTime(new Date());
        airCraft.setUpdateTime(new Date());
        int insert = airCraftMapper.insert(airCraft);
        if (insert > 0) {
            list.add(airCraft);
            return airCraft;

        }
        throw new RuntimeException("添加飞机失败");
    }

    private void ValidAirPort(AirCraft airCraft) {
        if (airCraft == null) {
            throw new RuntimeException("飞机信息不能为空");
        }
    }

    public void existingRegister(String registration) {
        for (AirCraft register : list) {
            if (register.getRegistration().equals(registration)) {
                throw new RuntimeException("飞机注册号用过了不能重复用");
            }

        }

    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("飞机id为空");
        }
        int delete = airCraftMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new RuntimeException("删除失败 ! id:" + id);
    }

    @Override
    public AirCraft edit(AirCraft edit) {
        ValidAirPort(edit);
        String editId = edit.getId();
        if (editId.isEmpty()) {
            throw new RuntimeException("id不能没有");
        }
        String editCover=edit.getRegistration();
        if (StrUtil.isBlank(editCover) || editCover.length() > 7) {
            throw new RuntimeException("飞机注册号不能超过7位且不能为空");
        }
        edit.setUpdateTime(new Date());
        existingRegister(edit.getRegistration());
        int editAir = airCraftMapper.updateById(edit);
        if (editAir > 0) {
            list.add(edit);
            return edit;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public AirCraft findAirPort(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("飞机id不能没有");
        }
        return airCraftMapper.selectById(id);
    }

    @Override
    public PageVo<AirCraft> pageVo(Integer page, Integer pageSize
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirCraft> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<AirCraft> pageParam = new Page<>(page, pageSize);
        Page<AirCraft> userPage = airCraftMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);

    }
}
